package net.qiqbframework.event.dao.mfimpl;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import net.qiqbframework.common.dto.PageResultDto;
import net.qiqbframework.common.utils.StringUtils;
import net.qiqbframework.eventsourcing.dao.DomainEventDao;
import net.qiqbframework.eventsourcing.dao.DomainEventPageCondition;
import net.qiqbframework.eventsourcing.dao.DomainEventPo;
import net.qiqbframework.mf.BasicEntityPoDaoMfImpl;
import net.qiqbframework.mf.MpUtil;

import java.util.List;

public class DomainEventDaoMfImpl extends BasicEntityPoDaoMfImpl<DomainEventMapper, DomainEventPo> implements DomainEventDao {


    @Override
    public List<DomainEventPo> listSameAggregateRootReadyById(String id) {
        final DomainEventPo domainEventPo = getPoById(id);
        /*if (domainEventPo == null || !domainEventPo.getNoticeStatus().equals(NoticeStatus.ready.name())) {
            return new ArrayList<>();
        }*/
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper
                //.eq(DomainEventPo::get NoticeStatus, NoticeStatus.ready.name())
                //.eq(StringUtils.isNotEmpty(domainEventPo.getNamespaceName()), DomainEventPo::getNamespaceName, domainEventPo.getNamespaceName())
                //.eq(StringUtils.isNotEmpty(domainEventPo.getApplicationName()), DomainEventPo::getApplicationName, domainEventPo.getApplicationName())
                .eq(DomainEventPo::getAggregateIdentifier, domainEventPo.getAggregateIdentifier(), StringUtils.nonEmptyOrNull(domainEventPo.getAggregateIdentifier()))
                .eq(DomainEventPo::getAggregateName, domainEventPo.getAggregateName(), StringUtils.nonEmptyOrNull(domainEventPo.getAggregateName()))
                .orderBy(DomainEventPo::getCreateTime, false)
        ;
        return super.list(queryWrapper);
    }

    @Override
    public DomainEventPo getPoById(String id) {
        return super.getById(id);
    }

    @Override
    public PageResultDto<DomainEventPo> list4Page(DomainEventPageCondition condition) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper
                .like(DomainEventPo::getId, condition.getId(), StringUtils.nonEmptyOrNull(condition.getId()))
                //.like(StringUtils.isNotEmpty(condition.getPayloadParam()), DomainEventPo::getPayloadParam, condition.getPayloadParam())
                .like(DomainEventPo::getPayloadType, condition.getPayloadClassName(), StringUtils.nonEmptyOrNull(condition.getPayloadClassName()))
                //.eq(StringUtils.isNotEmpty(condition.getStatus()), DomainEventPo::getNoticeStatus, condition.getStatus())
                .orderBy(DomainEventPo::getCreateTime, false)
        ;
        final Page<DomainEventPo> page = super.page(MpUtil.ofPage(condition), queryWrapper);
        return MpUtil.toResult(page);
    }
}
